/**
 * 收款账户历史填写回填
 *
*/
import { accountList } from '@/api/supplier';
import { getHistoryAccounts, deleteAccount } from '@/api/loan';
export default {
  data() {
    return {
    };
  },
  created() {

  },
  methods: {
    fillAccountInfo() {
      const params = {
        page: 1,
        perPage: 100
      };
      getHistoryAccounts(params).then(res => {
        if (res.code !== 200) {
          this.$message.error(res.message);
          return;
        }
        if (res.data.items && res.data.items.length) {
          res.data.items[0].province = res.data.items[0].province.replace(/-/g, ',');
          this.accountInfo = [res.data.items[0]];
        }
      }).catch(err => {
        console.log(err);
      });
    },
    autocompleteDelFn({ id }) {
      deleteAccount(id).then(res => {
        if (res.code !== 200) {
          this.$message.error(res.message);
          return;
        }
        this.$message.success('删除成功');
      }).catch(err => {
        console.log(err);
      });
    },
    /**
     * el-autocomplete事件 historyAccountType 0个人账户 1供应商账户
     */
    fetchAutocomplete({ value, cb, item, settingItem }) {
      if (settingItem.keyword === 'payName') {
        const supplierId = Number(this.supplierId);
        if (this.historyAccountType && !this.supplierId) { cb([]); return; }
        const supplierParams = {
          page: 1,
          perPage: 100,
          type: 1, // 飞博供应商
          id: supplierId,
          supplierId: supplierId
        };
        const personalParams = {
          page: 1,
          perPage: 100
        };
        const params = this.historyAccountType === 0 ? personalParams : supplierParams;
        if (value) {
          params[`${settingItem.queryName ? settingItem.queryName : 'payName'}`] = value;
        }
        const listApi = this.historyAccountType === 0 ? getHistoryAccounts : accountList;
        listApi(params).then(res => {
          if (res.code === 200) {
            res.data.items.map(i => {
              const str = i.payNum.slice(-4);
              i.value = `${i.payName}(${str})`;
              if (i.province) {
                i.province = i.province.replace(/-/g, ',');
              }
            });
            cb(res.data.items);
          } else {
            cb([]);
          }
        }).catch(() => {
          cb([]);
        });
      }
    },
    autocompleteSelect({ value, item, settingItem }) {
      if (settingItem.keyword === 'payName') {
        const selectItem = _.cloneDeep(value);
        const keys = this.historyAccountType === 0 ? ['payName', 'subBank', 'bank', 'province', 'payNum'] : ['payName', 'payNum', 'tel', 'subBank', 'bank', 'province', 'identityNumber'];
        for (const i of keys) {
          this.$set(item, i, selectItem[i]);
        }
      }
    }
  }
};
